home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / sorting / ssort / frmsort1.frm (.txt) next >
Encoding:
Visual Basic Form  |  1995-06-30  |  3.6 KB  |  130 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "Shell Sort Example"
  4.    ClientHeight    =   4470
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1485
  7.    ClientWidth     =   7365
  8.    Height          =   4875
  9.    Left            =   1035
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4470
  12.    ScaleWidth      =   7365
  13.    Top             =   1140
  14.    Width           =   7485
  15.    Begin ListBox List3 
  16.       Height          =   2955
  17.       Left            =   4080
  18.       Sorted          =   -1  'True
  19.       TabIndex        =   2
  20.       Top             =   465
  21.       Width           =   1755
  22.    End
  23.    Begin ListBox List2 
  24.       Height          =   2955
  25.       Left            =   2175
  26.       TabIndex        =   1
  27.       Top             =   465
  28.       Width           =   1755
  29.    End
  30.    Begin ListBox List1 
  31.       Height          =   2955
  32.       Left            =   180
  33.       TabIndex        =   0
  34.       Top             =   480
  35.       Width           =   1755
  36.    End
  37.    Begin Label Label1 
  38.       Caption         =   "Sorted ListBox"
  39.       Height          =   255
  40.       Index           =   2
  41.       Left            =   4080
  42.       TabIndex        =   5
  43.       Top             =   195
  44.       Width           =   1755
  45.    End
  46.    Begin Label Label1 
  47.       Caption         =   "Sorted List"
  48.       Height          =   255
  49.       Index           =   1
  50.       Left            =   2160
  51.       TabIndex        =   4
  52.       Top             =   180
  53.       Width           =   1755
  54.    End
  55.    Begin Label Label1 
  56.       Caption         =   "Unsorted List"
  57.       Height          =   255
  58.       Index           =   0
  59.       Left            =   195
  60.       TabIndex        =   3
  61.       Top             =   180
  62.       Width           =   1755
  63.    End
  64. ' Shell Sort routine for Visual Basic
  65. ' Harry JF Wykes, End User Computing Limited, 100014.2573@compuserve.com
  66. ' Sorts an array of strings into ascending order.
  67. ' Can be easily modified to deal with arrays of other types
  68. ' including user defined types.
  69. ' The sort can be made ascending by modifying the string
  70. ' comparison operator.
  71. ' Ported from C when I needed to sort an array of 12000 strings
  72. ' and a Sorted Listbox bombed out after 2000-odd records were added
  73. ' to it.
  74. ' I made a beeline for the VB forum, searched the libraries for 'SORT'
  75. ' and was astonished to find not a thing.  So if anyone else needs a
  76. ' VB sort routine, here it is.
  77. ' The sort is a Shell Sort.  For more details see Robert Sedgewick's
  78. ' excellent book 'Algorythms'
  79. Sub Form_Load ()
  80.     Dim S() As String
  81.     ReDim S(12) As String
  82.     S(1) = "One"
  83.     S(2) = "Two"
  84.     S(3) = "Three"
  85.     S(4) = "Four"
  86.     S(5) = "Five"
  87.     S(6) = "Six"
  88.     S(7) = "Seven"
  89.     S(8) = "Eight"
  90.     S(9) = "Nine"
  91.     S(10) = "Ten"
  92.     S(11) = "Eleven"
  93.     S(12) = "Twelve"
  94.     T% = 12
  95.     For Ctr% = 1 To T%
  96.         List1.AddItem S(Ctr%)
  97.     Next Ctr%
  98.     ShellSort S()
  99.     For Ctr% = 1 To T%
  100.         List2.AddItem S(Ctr%)
  101.     Next Ctr%
  102.     For Ctr% = 1 To T%
  103.         List3.AddItem S(Ctr%)
  104.     Next Ctr%
  105. End Sub
  106. Sub ShellSort (S() As String)
  107. ' Sort the array S
  108. Dim Gap As Integer
  109. Dim i As Integer
  110. Dim j As Integer
  111. Dim n As Integer
  112. n = UBound(S)
  113. Gap = n / 2
  114. Do While Gap > 0
  115.     i = Gap
  116.     For i = Gap To n
  117.         j = i - Gap
  118.         Do While j > 0
  119.             If S(j) < S(j + Gap) Then
  120.                 Exit Do
  121.             End If
  122.             T$ = S(j)
  123.             S(j) = S(j + Gap)
  124.             S(j + Gap) = T$
  125.             j = j - Gap
  126.         Loop
  127.     Next i
  128.     Gap = Gap / 2
  129. End Sub
  130.